package com.dao.imp;

import com.dao.LoginDao;
import com.entity.Merchant;
import com.util.DBConn;

import java.sql.ResultSet;

/**
 * @author Mathison
 * @Title: MerchantLogin
 * @Package: com.dao.imp
 * @Description: TODO
 * @date 2021/11/22 16:17
 **/
public class MerchantLogin implements LoginDao {
    private boolean loginResult = false;
    private Merchant merchant = null;
    public Merchant getMerchant(){
        return this.merchant;
    }

    public boolean isLoginResult() {
        return loginResult;
    }

    @Override
    public boolean login(String userId, String password){
//        username会匹配到email或ID
        String sql = "select distinct * from merchants where (merchant_email='" + userId +"'and merchant_password='"+password+ "')or(merchant_id='"+userId+"' and merchant_password='"+password+"')";
//        System.out.println("sql:" + sql);
        tryLogin(sql);
        return this.loginResult;

    }
    @Override
    public Merchant loginResult(String username, String password){
        login(username,password);
        return this.merchant;
    }

    @Override
    public Merchant getUserInfo(String userId){
        String sql = "select  * from merchants where merchant_id=" + userId ;
        System.out.println(sql);
        tryLogin(sql);
        return this.merchant;
    }

    private void tryLogin(String sql) {
        Merchant merchant=new Merchant();

        try {
            DBConn.init();
            ResultSet rs = DBConn.selectSql(sql);
            while (rs.next()) {
                merchant.setMerchant_id(rs.getInt("merchant_id"));
                merchant.setMerchant_email(rs.getString("merchant_email"));
                merchant.setMerchant_nickname(rs.getString("merchant_nickname"));
                merchant.setMerchant_password(rs.getString("merchant_password"));
                merchant.setMerchant_storeName(rs.getString("merchant_address"));
            }
            DBConn.closeConn();
        } catch (Exception e) {
            //
        }
//        System.out.println(merchant.getMerchant_id());
        if(merchant!=null){
            this.loginResult=true;
        }
        this.merchant =merchant;

    }



}
